package concurrent;

import java.util.concurrent.CountDownLatch;

public class CountDownLatchDemo {


    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(3);

       new Thread(()->{
           System.out.println("线程1开始开始执行");
           try{
               Thread.sleep(2000);
           }catch (Exception e){
               e.printStackTrace();
           }
           System.out.println("线程1执行完毕");
           countDownLatch.countDown();
       }).start();

       new Thread(()->{
           System.out.println("线程2开始开始执行");
           try{
               Thread.sleep(2000);
           }catch (Exception e){
               e.printStackTrace();
           }
           System.out.println("线程2执行完毕");
           countDownLatch.countDown();
       }).start();

       new Thread(()->{
           System.out.println("线程3开始开始执行");
           try{
               Thread.sleep(2000);
           }catch (Exception e){
               e.printStackTrace();
           }
           System.out.println("线程3执行完毕");
           countDownLatch.countDown();
       }).start();

       countDownLatch.await();
        System.out.println("主线程执行完毕");
    }
}
